package com.dg11185.nearshop.baidumap;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.nearshop.R;
import com.baidu.mapapi.map.BaiduMap.OnMapLoadedCallback;
import com.baidu.mapapi.map.BaiduMap.OnMapStatusChangeListener;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.model.LatLng;
import com.dg11185.libs.network.NetClient;
import com.dg11185.nearshop.app.ConvertUnit;
import com.dg11185.nearshop.app.MyApplication;
import com.dg11185.nearshop.app.ViewUtils;
import com.dg11185.nearshop.baidumap.ReverseGeocode.ReverseListener;
import com.dg11185.nearshop.net.request.GeneralRequest;
import com.dg11185.nearshop.net.response.NetStatus;
import com.dg11185.nearshop.view.MyDialog;

/**
 * 错误反馈界面
 * 
 * @author：Daibo
 * @time：2014-7-15 上午11:21:30
 */
public class ErrorFeedbackActivity extends BaseShopActivity {

    private TextView poiTxt;

    private LinearLayout dataLayout, loadingLayout;

    private ReverseGeocode geocode = null;

    private LatLng curPoi = null;

    private Dialog dialog = null;

    @Override
    public void setContentView() {
        setContentView(R.layout.map_error_feedback);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initShopOverlay();
        geocode = new ReverseGeocode(reverseListener);
        mBaiduMap.setOnMapLoadedCallback(loadedListener);
        poiTxt = (TextView) this.findViewById(R.id.map_choice_position);
        dataLayout = (LinearLayout) this.findViewById(R.id.map_choice_layout);
        loadingLayout = (LinearLayout) this.findViewById(R.id.map_choice_loading);

        poiTxt.setText("调整好位置后点击确定提交..");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /** 地图加载完毕的回调方法 */
    private OnMapLoadedCallback loadedListener = new OnMapLoadedCallback() {

        @Override
        public void onMapLoaded() {
            mBaiduMap.setOnMapStatusChangeListener(statusListener);
        }
    };

    /**
     * 地图状态改变的回调方法
     */
    private OnMapStatusChangeListener statusListener = new OnMapStatusChangeListener() {

        @Override
        public void onMapStatusChangeStart(MapStatus arg0) {
        }

        @Override
        public void onMapStatusChangeFinish(MapStatus arg0) {

            curPoi = mBaiduMap.getMapStatus().target;
            geocode.reverseGeoCode(curPoi);
            dataLayout.setVisibility(View.GONE);
            loadingLayout.setVisibility(View.VISIBLE);
        }

        @Override
        public void onMapStatusChange(MapStatus arg0) {
        }
    };

    /**
     * 反地理编码查询结束回调方法
     */
    private ReverseListener reverseListener = new ReverseListener() {

        @Override
        public void onGetAddrResult(String str) {
            poiTxt.setText(str);
            loadingLayout.setVisibility(View.GONE);
            dataLayout.setVisibility(View.VISIBLE);
        }
    };

    /**
     * button点击事件 提交修改
     */
    public void onSubmitClicked(View v) {

        if (curPoi == null) {
            return;
        }
        dialog = new MyDialog.Builder(this).setLayout(R.layout.map_error_feedback_dialog).setTitle(shopData.shopTitle)
                .setMessage(shopData.shopAddress).setScale(0.75).setPositiveButton(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        submitFeedback();
                        dialog.dismiss();
                    }
                }).setNegativeButton(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                }).create();
        TextView curAddr = (TextView) dialog.findViewById(R.id.feedback_new_addr);
        curAddr.setText(poiTxt.getText().toString());
        dialog.show();
    }

    /**
     * 提交新地理位置
     */
    private void submitFeedback() {

        final Dialog loadingDialog = ViewUtils.createSubmittingDialog(this);
        loadingDialog.show();

        GeneralRequest request = new GeneralRequest(GeneralRequest.LOCATION_UPDATE);
        if (MyApplication.getUser() != null) {
            request.add("updateName", ConvertUnit.getFullUserName());
        }
        request.add("merchantId", shopData.shopId).add("merchantName", shopData.shopTitle)
                .add("latitude", curPoi.latitude + "").add("longitude", curPoi.longitude + "").encryption();

        request.setActionListener(new GeneralRequest.ActionListener<NetStatus>() {

            @Override
            public void onSuccess(NetStatus result) {
                loadingDialog.dismiss();
                if (result == NetStatus.SUCCESS) {
                    ViewUtils.showToast(ErrorFeedbackActivity.this, "提交成功，感谢您的参与~~");
                } else {
                    onFailure(-1);
                }
            }

            @Override
            public void onFailure(int resultCode) {
                loadingDialog.dismiss();
                if (resultCode == NetClient.NET_DISCONNECT_CODE) {
                    ViewUtils.showToast(ErrorFeedbackActivity.this, R.string.cant_connect_network);
                } else {
                    ViewUtils.showToast(ErrorFeedbackActivity.this, R.string.submit_error);
                }
            }
        });
        NetClient.httpPost(request);
    }

}
